﻿using GENERIC.MVVM.Helpers;
using GENERIC.MVVM.Interfaces.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GENERIC.MVVM.ViewModels
{
    /// <summary>
    /// ViewModelFactory de base
    /// </summary>
    public abstract class ViewModelFactory
    {
        protected static IViewModelResolver resolver = null;

        public static void InitializeResolver(IViewModelResolver resolver)
        {
            ViewModelFactory.resolver = resolver;
        }
    }

    /// <summary>
    /// ViewModelFactory generic avec le type du viewModel
    /// </summary>
    /// <typeparam name="TViewModelType">Type du viewModel</typeparam>
    public abstract class ViewModelFactory<TViewModelType> : ViewModelFactory where TViewModelType : IViewModel
    {
        #region Properties

        /// <summary>
        /// Obtient le viewModel
        /// </summary>
        public virtual TViewModelType ViewModel
        {
            get
            {
                if (resolver == null)
                {
                    throw new InvalidOperationException();
                }
                var viewModel = resolver.Resolve<TViewModelType>();
                viewModel.OnConstructed();
                return viewModel;
            }
        }

        #endregion
    }

    /// <summary>
    /// ViewModelFactory generic avec le type du viewModel et du DesignViewModel
    /// </summary>
    /// <typeparam name="TViewModel">Type du viemModel</typeparam>
    /// <typeparam name="TDesignViewModel">Type du viewModel pour le designer</typeparam>
    /// <typeparam name="TModelType">Type du modèle</typeparam>
    public abstract class ViewModelFactory<TViewModel, TDesignViewModel, TModelType> : ViewModelFactory<TViewModel>
        where TViewModel : IViewModel
        where TDesignViewModel : TViewModel, new()
    {
        #region Members

        private readonly bool _DesignMode;

        #endregion
        
        #region Consturctor

        public ViewModelFactory()
        {
            _DesignMode = DesignHelpers.DesignMode;
        }

        #endregion

        #region Properties

        #endregion

        public override TViewModel ViewModel
        {
            get
            {
                TViewModel viewModel;
                if (_DesignMode)
                {
                    viewModel = new TDesignViewModel();
                    viewModel.OnConstructed();
                }
                else
                {
                    viewModel = base.ViewModel;
                }
                return viewModel;
            }
        }

        #region Methods

        

        #endregion
    }
}
